//
//  SINAStuApproveSelectSchoolViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/29.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAStuApproveSelectSchoolViewController.h"
#import "SINAStuSelectSchoolTableViewCell.h"
#import "SINASchoolModel.h"
#import "SINAStuApproveStudentViewController.h"
#import "SINAHeaderSearchBar.h"

@interface SINAStuApproveSelectSchoolViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (strong, nonatomic)  UITableView *mainContentTableView;
@property (strong, nonatomic)  UILabel *currentSchoolLabel;
@property (strong, nonatomic) SINAHeaderSearchBar *headerSearchBar;
@property(nonatomic,strong) NSMutableArray *dataList;
@property(nonatomic,strong) NSMutableArray *searchList;
//1 所有的学校
//2 搜索的学校
@property(nonatomic,assign) NSInteger dataType;
@end

@implementation SINAStuApproveSelectSchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super initBackItem];
    [self initViewFunction];
}

-(void) initViewFunction{
    
    self.navigationItem.title=@"选择学校";
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
    self.dataType=1;
    
    //导航条的搜索条
    SINAHeaderSearchBar *searchBar = [[SINAHeaderSearchBar alloc]initWithFrame:CGRectMake(0.0f,0.0f,mainW-120,40.0f)];
    searchBar.delegate = self;
    searchBar.searchBarStyle = UISearchBarStyleDefault;
    searchBar.placeholder = @"";
    [searchBar setShowsCancelButton:NO];
    [searchBar setTintColor:[UIColor whiteColor]];
    CGFloat height = searchBar.bounds.size.height;
    CGFloat top = (height - 31.0) / 2.0;
    CGFloat bottom = top;
    
    searchBar.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    
    self.headerSearchBar = searchBar;
    
    
    [searchBar setPlaceholder:@"搜索"];
    
    //将搜索条放在一个UIView上
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainW-120, 40)];
    searchView.backgroundColor = [UIColor clearColor];
    [searchView addSubview:searchBar];
    
    self.navigationItem.titleView = searchView;
    
    UIView *rightView =[[UIView alloc]initWithFrame:CGRectMake(0,0,40,50)];
    UIButton *rightButton =[[UIButton alloc]initWithFrame:CGRectMake(0,0,40,45)];
    [rightButton addTarget:self action:@selector(startSearchFunction) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:rightButton];
    [rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonView = [[UIBarButtonItem alloc]initWithCustomView:rightView ];
    self.navigationItem.rightBarButtonItem=rightButtonView;
    
    
    UIView *itemView1 =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 22, mainW, 48)];
    [itemView1 setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:itemView1];
    
    
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetHeight(itemView1.frame)/2-10, mainW-30, 21)];
    titleLabel.text=@"当前学校";
    [titleLabel setFont:[UIFont systemFontOfSize:16]];
    titleLabel.textColor=[UIColor colorWithHexString:@"#2e2e2e"];
    
    [itemView1 addSubview:titleLabel];
    self.currentSchoolLabel = titleLabel;
    if (self.currentSchooleModel!=nil) {
        self.currentSchoolLabel.text=[NSString stringWithFormat:@"当前学校 %@",self.currentSchooleModel.schoolName];
    }
    
    
    self.mainContentTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(itemView1.frame)+20, mainW, mainH-CGRectGetMaxY(itemView1.frame)-20)];
    self.mainContentTableView.delegate=self;
    self.mainContentTableView.dataSource=self;
    self.mainContentTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.mainContentTableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.mainContentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mainContentTableView];
    for (NSInteger i=0; i<0; i++) {
        SINASchoolModel *model = [[SINASchoolModel alloc]init];
        model.schoolName=@"太原师范学院";
        [self.dataList addObject:model];
    }
    
    SINASchoolModel *model = [[SINASchoolModel alloc]init];
    model.schoolName=@"暂无学校信息";
    [self.dataList addObject:model];
    
    [self.mainContentTableView reloadData];
}
//开始搜索
-(void)startSearchFunction{
    [self.view endEditing: YES];
    NSString *searchText =  self.headerSearchBar.text;
    if (searchText==nil||searchText.length<1) {
        WISAlertView *alertView =
        [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入搜索的内容" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    //搜索大学
    self.dataType =2;
    [self searchAllUniversterFunction:searchText];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    DLog(@"searchBarTextDidEndEditing");
}
-(void)viewWillAppear:(BOOL)animated{
    if (self.currentSchooleModel!=nil) {
        self.currentSchoolLabel.text=[NSString stringWithFormat:@"当前学校 : %@",self.currentSchooleModel.schoolName];
    }
    WeakSelf;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf loadingAllUniversterFunction];
    });
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}
-(NSMutableArray *)searchList{
    if (!_searchList) {
        _searchList = [NSMutableArray array];
    }
    return  _searchList;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataType==1) {
         return self.dataList.count;
    } else {
         return self.searchList.count;
    }
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SINAStuSelectSchoolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINAStuSelectSchoolTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINAStuSelectSchoolTableViewCell" owner:nil options:nil] firstObject];
        
    }
    
    
    SINASchoolModel *model=nil;
    if (self.dataType==1) {
        model=self.dataList[indexPath.row];
    } else {
        model=self.searchList[indexPath.row];
    }
    
    cell.schoolModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.pos=indexPath.row;
    WeakSelf;
    cell.itemClickBlock = ^(NSInteger pos) {
        SINASchoolModel *model=nil;
        if (weakSelf.dataType==1) {
            model=weakSelf.dataList[indexPath.row];
        } else {
            model=weakSelf.searchList[indexPath.row];
        }
        SINAStuApproveStudentViewController *power= [weakSelf.navigationController.viewControllers objectAtIndex:weakSelf.navigationController.viewControllers.count-2];
        power.currentSchooleModel =model;
        [weakSelf.navigationController popToViewController:power animated:YES];
    };
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 46;
}
//查看详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    
}

//搜索所有的大学
-(void) searchAllUniversterFunction:(NSString*) schoolName{
    
    [self startLoading];
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?app=1&name=%@",baseUrl,allUniversityUrl,schoolName] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [self.searchList removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINASchoolModel *model = [[SINASchoolModel alloc]init];
                    model.schoolName=dic[@"name"];
                    model.schoolId=dic[@"id"];
                    [self.searchList addObject:model];
                }
                
            }else{
                NSString *message =@"无搜索大学信息";
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                     cancleBtn:nil];
                [alertView showXLAlertView];
                self.dataType=1;
            }
            
            [self.mainContentTableView reloadData];
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
    
}

//加载所有的大学
-(void) loadingAllUniversterFunction{
    
    [self startLoading];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?app=1",baseUrl,allUniversityUrl] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [self.dataList removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINASchoolModel *model = [[SINASchoolModel alloc]init];
                    model.schoolName=dic[@"name"];
                    model.schoolId=dic[@"id"];
                    [self.dataList addObject:model];
                }
                
            }else{
                [self.dataList removeAllObjects];
                SINASchoolModel *model = [[SINASchoolModel alloc]init];
                model.schoolName=@"暂无学校信息";
                [self.dataList addObject:model];
            }
            
            [self.mainContentTableView reloadData];
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
